define(['jquery'], function () {
    var mailArr = [
        "@qq.com",
        "@126.com",
        "@163.com",
        "@sina.com",
        "@sohu.com",
        "@gmail.com",
        "@hotmail.com"];

    $.fn.mailAutoTip = function (options) {
        //下拉框DOM
        var inputObj = this;

        var setting = {
            subBox: '.MailAutoTip',    //下拉框div
            subOp: "li",
            inputObj: inputObj,   //email输入框的id属性,在其他地方调用需要在此添加输入框属性.
            mailArr: mailArr,
            hoverClass: "on",
            _cur: 0 /*index*/,
            suffixTip: '请选择邮箱类型',
            inputWidth: $(inputObj).width(),
            inputHeight: $(inputObj).height()
        };

        //浅拷贝参数
        var opts = $.extend({}, setting, options || {});

        //插入邮箱提示框,放到body后
        if ($(opts.subBox).size() == 0) {
            $('body').append($('<div class="MailAutoTip"></div>'));
        }

        function getElementLeft(elem) {
            var actualLeft = 0, current;

            current = elem;
            while (current) {
                actualLeft += current.offsetLeft;
                current = current.offsetParent;
            }

            return actualLeft;
        }

        function getElementTop(elem) {
            var actualTop = 0, current, parent;

            current = elem;
            while (current) {
                actualTop += current.offsetTop;

                parent = current.offsetParent;
                //如果当前元素是document或者是绝对定位的元素
                if (parent == null) {
                    if (current != document.body) {
                        actualTop += document.body.scrollTop;
                        actualTop -= current.scrollTop;
                    }
                }

                current = parent;
            }

            return actualTop;
        }

        /*function getElementTop(elem){
         var actualTop = elem.offsetTop;
         var current = elem.offsetParent;

         while (current){
         actualTop += current.offsetTop;
         current = current.offsetParent;
         }

         return actualTop;
         }*/

        /**
         * 显示输入提示框
         * @param _v 输入的值
         * @param o 输入框对象
         */
        function tipFun(_v, o) {
            opts._cur = 0;
            var _that = o;

            //动态获得提示框的位置，并显示
            /*  var left = getElementLeft(opts.inputObj[0]),
             top = getElementTop(opts.inputObj[0]);*/
            var left =(opts.inputObj[0]).getBoundingClientRect().left,
                top = (opts.inputObj[0]).getBoundingClientRect().top;

            /* $(opts.subBox).css("width", opts.inputWidth + 'px');*/
            $(opts.subBox).css("width",(_that[0].clientWidth +2)+ 'px');

            $(opts.subBox).css("left", left + 'px');
            $(opts.subBox).css("top", (top + parseInt(opts.inputHeight)) + 'px');
            $(opts.subBox).show();

            var str = "<ul>";
            str += "<li class='' =\"e_type\">" + opts.suffixTip + "</li>";
            var e = _v.indexOf("@");
            if (e == -1) {
                $.each(opts.mailArr, function (s, m) {
                    str += '<li><a href="javascript:void(0)" >' + _v + m + "</a></li>";
                });
            } else {
                var _sh = _v.substring(0, e)
                var _se = _v.substring(e);
                var ind = 0;
                $.each(opts.mailArr, function (s, m) {
                    if (m.indexOf(_se) != -1) {
                        str += '<li><a href="javascript:void(0)" >' + _sh + m + "</a></li>";
                        ind = 1;
                    }
                });
                if (ind == 0) {
                    str += '<li><a class="cur_val" href="javascript:void(0)" >' + _v + "</a></li>";
                }
            }
            str += "</ul>";
            $(opts.subBox).html(str);

            /*hover*/
            $(opts.subBox).find(opts.subOp).hover(function () {
                var _that = $(this);
                _that.addClass(opts.hoverClass);
            }, function () {
                var _that = $(this);
                _that.removeClass(opts.hoverClass)
            });

            /*click*/
            $(opts.subBox).find(opts.subOp).click(function (e) {
                if ($(e.target).attr("id") != "e_type") {
                    $(opts.inputObj).val($(e.target).html());
                    if(opts.onHide){
                        opts.onHide( $(opts.inputObj).val());
                    }
                    $(opts.subBox).hide();
                    e.stopPropagation();
                }
            });
        }

        /**
         * 根据键盘上下键来切换显示项
         */
        function itemFun() {
            console.log('document keydown keyup');

            var _tempArr = $(opts.subBox).find(opts.subOp);
            var _size = _tempArr.size();
            _tempArr.removeClass(opts.hoverClass);
            if (_size > 1) {
                if (opts._cur > _size - 1) {
                    opts._cur = 1;
                }
                if (opts._cur < 1) {
                    opts._cur = _size - 1;
                }
                _tempArr.eq(opts._cur).addClass(opts.hoverClass);
            } else {
                opts._cur = 1;
            }
        }

        $(opts.inputObj).unbind('keyup');
        $(opts.inputObj).bind('keyup', function (e) {
            var _that = $(this);
            if (_that.val() != "") {
                if (e.keyCode != 38 && e.keyCode != 40 && e.keyCode != 13 && e.keyCode != 27) {
                    var _inputVal = _that.val();
                    tipFun(_inputVal, _that);
                }
            } else {
                $(opts.subBox).hide();
            }
        });

        var docClickFun = function (e) {
            $(opts.subBox).hide();
        };
        var docKeyDownFun = function (e) {
            if ($(opts.subBox).is(':hidden')) return;

            switch (e.keyCode) {
                case 40:
                    //down
                    opts._cur++;
                    itemFun()
                    break;
                case 38:
                    //up
                    opts._cur--;
                    itemFun()
                    break;
                default:
                    break;
            }
        };
        var inputKeyDownFun = function (e) {
            var _temp = $(opts.subBox).find(opts.subOp);
            if (e.keyCode == 13) {
                if (opts._cur != 0) {
                    $(this).val(_temp.eq(opts._cur).text());
                    if(opts.onHide){
                        opts.onHide( $(this).val());
                    }
                    opts._cur = 0;
                }
                $(opts.subBox).hide();

                //e.stopPropagation();
                return false;
            }
        };

        $(document).unbind("click", docClickFun);
        $(document).bind("click", docClickFun);

        $(document).unbind('keydown', docKeyDownFun);
        $(document).bind('keydown', docKeyDownFun);

        $(opts.inputObj).unbind('keydown', inputKeyDownFun);
        $(opts.inputObj).bind('keydown', inputKeyDownFun);
    }
});